package com.meixiang.activity.homes.service;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.google.gson.reflect.TypeToken;
import com.lzy.okhttputils.model.HttpParams;
import com.meixiang.R;
import com.meixiang.adapter.service.EvaluateAdapter;
import com.meixiang.entity.EvaluateGradle;
import com.meixiang.entity.training.TrainingEvaluate;
import com.meixiang.global.Config;
import com.meixiang.global.GlobalConventionalData;
import com.meixiang.global.GlobalType;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseActivity;
import com.meixiang.main.MXApplication;
import com.meixiang.util.AbJsonUtil;
import com.meixiang.util.AbStrUtil;
import com.meixiang.view.StarLayout;
import com.meixiang.view.swipetoloadlayout.base.OnLoadMoreListener;
import com.meixiang.view.swipetoloadlayout.base.OnRefreshListener;
import com.meixiang.view.swipetoloadlayout.base.SwipeToLoadLayout;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 项目-服务评价
 * xiejingwen
 */
public class ServiceEvaluateActivity extends BaseActivity {

    @Bind(R.id.star_layout)
    StarLayout starLayout;
    @Bind(R.id.swipe_target)
    RecyclerView swipeTarget;
    @Bind(R.id.refresh)
    SwipeToLoadLayout refresh;
    private String type;
    //美容培训
    private String o2oTrainId;
    //服务评价
    private String o2oServiceId;
    private int totalPage;
    private EvaluateAdapter mAdapter;
    private int pageNo=1;
    private boolean isRefresh=true;
    private List<EvaluateGradle> gradleList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_service_evaluate);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initTitle() {
        type=getIntent().getStringExtra("type");
        if(!AbStrUtil.isEmpty(type)
                &&GlobalType.TRAIN_EVALUATE_TYPE.equals(type)){
            setTitle("课程评价");
        }else{
            setTitle("服务评价");
        }
        gradleList = MXApplication.mApp.getEvaluateGradle();
        if(gradleList==null) {
            GlobalConventionalData.getEvaluateStart(this);
        }
    }

    @Override
    protected void initView() {
        refresh.setLoadingMore(false);
        swipeTarget.setLayoutManager(new LinearLayoutManager(context));
        mAdapter=new EvaluateAdapter(0);
        swipeTarget.setAdapter(mAdapter);
        o2oTrainId=getIntent().getStringExtra("o2oTrainId");
        o2oServiceId=getIntent().getStringExtra("o2oServiceId");
        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                isRefresh=false;
                pageNo++;
                if(pageNo>totalPage){
                    refresh.setLoadingMore(false);
                    return;
                }
               getTrainEvaluate();

            }
        });
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                isRefresh=true;
                pageNo = 1;
                refresh.setLoadMoreEnabled(false);
               getTrainEvaluate();
            }
        });
    }

    @Override
    protected void setData() {


//        starLayout.starOnclickListener(new StarLayout.IStarOnclick() {
//            @Override
//            public void starNumber(int number) {
//                AbToastUtil.showToast(context,"选择"+number);
//            }
//        });

        getTrainEvaluate();

    }

    /**
     * 获取美容培训评价内容
     */
    private void getTrainEvaluate(){
        if(isRefresh==true){
            status.showLoading();
        }
        String url;
        HttpParams params=new HttpParams();
        if (type.equals(GlobalType.SERVICE_EVALUATE_TYPE)){
            url= Config.SERVICE_EVALUATE;
            params.put("o2oServiceId",o2oServiceId);
        }else{
            url= Config.TRAINING_GET_EVALUATE_CONTENT;
            params.put("o2oTrainId",o2oTrainId);
        }
        params.put("pageNo",pageNo+"");
        Log.e("params",params.toString());
        HttpUtils.post(url, params, new HttpCallBack(this) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                onComplete();
                totalPage=result.optInt("totalPage");
                if(totalPage==0){
                    status.showNoData("没有评价");
                }else {
                    List<TrainingEvaluate> trainingEvaluates = AbJsonUtil.fromJson(result.optJSONArray("result").toString(), new TypeToken<ArrayList<TrainingEvaluate>>() {
                    });
                    int scoreAve = result.optInt("scoreAve");
                    mAdapter.addAll(trainingEvaluates);
                    starLayout.setStarNumber(scoreAve+"");
                    //评价规则暂时未定义好，未定
//                    starLayout.setStartText("惊喜");
                    if(gradleList!=null&&gradleList.size()>0) {
                        if (gradleList != null && gradleList.size() > 0) {
                            for (int i = 0; i < gradleList.size(); i++) {
                                if (scoreAve == Integer.parseInt(gradleList.get(i).getGrade())) {
                                    starLayout.setStartText(gradleList.get(i).getDescription());
                                }
                            }
                        }
                    }
                }
            }

            @Override
            public void onError(String code, String message) {
                onComplete();
                status.showNoNewWork(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        getTrainEvaluate();
                    }
                });
            }
        });

    }
    private void onComplete(){
        if(isRefresh==true){
            if(mAdapter.getItemCount()>0){
                mAdapter.clear();
            }
            refresh.setRefreshing(false);
            status.removeView();
        }else{
            refresh.setLoadingMore(false);
        }
    }
    @Override
    public void onClick(View v) {

    }
}
